package com.example.quickadapter


import android.util.SparseArray
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.view.menu.MenuView
import androidx.recyclerview.widget.RecyclerView
import com.witmed.lib_common.utils.ImageUtils

class BaseViewHolder(itemView:View) : RecyclerView.ViewHolder(itemView) {

    var list= SparseArray<View>()

    //根据index获取对应的view
    fun <V:View> getView(id:Int):V{
        var v:V?=null
        v= list.get(id) as V?
        if(v!=null){
            return v
        }
        v=find(id)
        if(v!=null)
            list.put(id,v)
        return v
    }

    /**
     * 根据id获取view
     */
    fun <V:View> find(id:Int):V{
        return itemView.findViewById(id)
    }

    //赋值
    fun setText(id:Int,value:String){
        if(value.isNullOrEmpty()){
            return
        }
        val textView = getView<TextView>(id)
        if(textView==null){
            return
        }
        textView.setText(value)
    }

    fun setImage(id:Int,resId:Int){
        val iv = getView<ImageView>(id)
        if(iv==null){
            return
        }
        iv.setImageResource(resId)
    }

    fun setImage(id:Int,path:String){
        val iv = getView<ImageView>(id)
        if(iv==null){
            return
        }
        //加载网络图片
        ImageUtils.loadIamge(iv.context,path,iv)
    }
}